package org.rever.split;

import java.util.ArrayList;
import java.util.Iterator;

class Range {
	private int begin;
	public Range(int begin, int end) {
		
		this.begin = begin;
		this.end = end;
	}
	private int end;
	
	public int getBegin() {
		return begin;
	}
	public int getEnd() {
		return end;
	}
	public void setBegin(int begin) {
		this.begin = begin;
	}
	public void setEnd(int end) {
		this.end = end;
	}
	public void tosting(){
	System.out.println("Range : " +begin+" "+end);
	}
}

public class SplitAndRange {
	
	int begin = -1;
	ArrayList<Range> split(String s){
		ArrayList<Range> setOfRanges = new ArrayList<Range>();
		int i;
		char[] ch = s.toCharArray();
		for ( i = 0; i < ch.length; i++) {
			
			if(begin == -1 && ch[i] != ' ' ){
				begin = i;
				
			}else if(begin != -1 && ch[i] == ' ' ){
				Range range = new Range(begin,i-1);
				setOfRanges.add(range);
				begin = -1;
			}
			
			
		}
		// If it reaches end of the string and range 
		// is not set then we will set at the end
		if(begin != -1 && i == ch.length){
			setOfRanges.add( new Range(begin,i));
		}

		return setOfRanges;

	}
	
	static void printRanges(ArrayList<Range> ranges) {
		for(int i = 0; i<ranges.size(); i++){
			System.out.println(ranges.get(i));
		}
		
	}
	public static void main(String[] args) {
		SplitAndRange ranges = new SplitAndRange();
		System.out.println(ranges.split("hello how"));
		
	}

}
